package com.wk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wk.dao.CmfzGuruDao;
import com.wk.dao.CmfzUserDao;
import com.wk.entity.CmfzGuru;
import com.wk.entity.CmfzUser;
import com.wk.service.CmfzUserService;
import com.wk.util.ThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class CmfzUserServiceImpl implements CmfzUserService {

    @Autowired
    private CmfzUserDao cmfzUserDao;
    @Autowired
    private CmfzGuruDao cmfzGuruDao;

    @Override
    public Map findByCondition(Integer page, Integer limit, String value) {

        Map map = new HashMap();


        Page<CmfzUser> cmfzUserPage = new Page<>(page,limit);

        Page page1 = cmfzUserDao.findByCondition(cmfzUserPage, value);
        List records = page1.getRecords();
        long total = page1.getTotal();

        map.put("code",0);
        map.put("msg","");
        map.put("data",records);
        map.put("count",total);

        return map;
    }

    @Override
    public Map addUser(CmfzUser cmfzUser) {
        Map map = new HashMap();
        map.put("data","添加失败");
        try{
            cmfzUserDao.insert(cmfzUser);
            map.put("data","添加成功");
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        return map;
    }

    @Override
    public Map findOne(Integer id) {
        Map map =new HashMap();
        CmfzUser cmfzUser = cmfzUserDao.selectById(id);
        List<CmfzGuru> cmfzGurus = cmfzGuruDao.selectList(null);
        map.put("data",cmfzUser);
        map.put("list",cmfzGurus);
        return map;
    }

    @Override
    public Map update(CmfzUser cmfzUser) {
        Map map = new HashMap();
        map.put("data","修改失败");
        try {
            cmfzUserDao.updateById(cmfzUser);
            map.put("data","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public List<Map> selectBySexCount() {

        List<Map> maps = cmfzUserDao.selectBySexCount();
        System.out.println(maps);
        return maps;
    }

    @Override
    public List<Map> RegisterIncrease() {

        return cmfzUserDao.RegisterIncrease();
    }

    @Override
    public List<Map> regionCount() {
        return cmfzUserDao.RegionCount();
    }

    @Override
    public Map selectByThread() {

        Map map = new HashMap();

        //创建计数器,参数为任务量
        CountDownLatch countDownLatch = new CountDownLatch(3);

        //创建子线程执行三个任务
        ThreadPoolExecutor executor = ThreadPollUtil.executor;

        //注册量查询
        executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> maps = RegisterIncrease();
                map.put("registerNum",maps);
                //计数器--
                countDownLatch.countDown();
            }
        });

        //男女比例查询
        executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> maps = selectBySexCount();
                map.put("SexNum",maps);
                countDownLatch.countDown();
            }
        });

        //中国地图查询
        executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> maps = regionCount();
                map.put("regionCount",maps);
                countDownLatch.countDown();
            }
        });

        //通过计数器让主线程等待
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return map;
    }
}
